package com.jayqqaa12.common;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.UUID;

import net.coobird.thumbnailator.Thumbnails;

import com.jayqqaa12.jbase.jfinal.ext.ctrl.JsonController;
import com.jayqqaa12.jbase.util.Sec;
import com.jayqqaa12.jbase.util.upload.FlashUpload;
import com.jayqqaa12.jbase.util.upload.KindEditor;
import com.jayqqaa12.model.json.SendJson;
import com.jfinal.ext.route.ControllerBind;
import com.jfinal.upload.UploadFile;

@ControllerBind(controllerKey = "/common/file")
public class FileCtrl extends JsonController {
	
	
	public void flashUpload(){
		renderJson(FlashUpload.flashUpload(getRequest()));
	}
	

	public void kindEidtorUpload() {
		renderJson(KindEditor.upload(this));
	}

	public void kindEidtorFileManage() {
		renderJson(KindEditor.fileManage(getRequest()));
	}

	
	/**
	 * 上传图片 自动缩放
	 * @throws FileNotFoundException
	 * @throws IOException
	 */
	public void uploadImg() throws FileNotFoundException, IOException {

		int code = 200;
		SendJson json = getJsonObject();
		UploadFile f = getFile();
		int w = getParaToInt("w");
		int h = getParaToInt("h");

		String fileName = Sec.MD5Encrypt(UUID.randomUUID().toString(), 16) + ".png";

		if (f.getFile().exists()) {
			Thumbnails.of(f.getFile()).size(w, h).keepAspectRatio(false).toFile(new File(f.getSaveDirectory()+File.separator + fileName));
			f.getFile().delete();
		}

		json.put("img", "/upload/" + fileName);

		json.code = code;
		sendJson(json);
	}
	
	
	
	
	
	
	
	
	
	

}
